﻿using UnityEngine;

namespace Yoozoo.Mars.Got.SpriteAnim
{
    public class AnimSprite
    {
        //现在都是通过这个来传递数据
        public AnimSpriteCreator ptSystemNode;
        public ParticleSystem.Particle particle;
        public int psIndex = -1;
        //自己在池子中的位置
        public int spriteIndex;
        public void SetSpriteIndex(int newSpriteIndex)
        {
            // 用于删除Sprite时，将列表末尾的Sprite替代，设置新的spriteIndex，并对各个属性重新赋值
            spriteIndex = newSpriteIndex;
            alpha = _alpha;
            action = _action;
            rotation = _rotation;
            speed = _speed;
            position = _position;
            offset = _offset;
            spriteSize = _spriteSize;
            ref var data = ref ptSystemNode.GetData();
            data.restarts[spriteIndex] = _needRefreshFlag || _changeAction;
        }
        
        //禁止外部调用使用
        public bool active = false;
        public float startLifetime = 0;
        public float animationPhase = 0f;

        private bool _changeAction;

        public bool changeAction
        {
            get => _changeAction;
            set
            {
                _changeAction = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.restarts[spriteIndex] = data.restarts[spriteIndex] || _changeAction;
                }
            }
        }

        private Vector3 _position = Vector3.zero;
        public Vector3 position
        {
            get => _position;
            set
            {
                _position = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.positions[spriteIndex] = _position;
                }
            }
        }

        private Vector3 _offset = Vector3.zero;

        public Vector3 offset
        {
            get => _offset;
            set
            {
                _offset = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.offsets[spriteIndex] = _offset;
                }
            }
        }

        private Vector3 _rotation = Vector3.zero;

        public Vector3 rotation

        {
            get => _rotation;
            set
            {
                _rotation = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.rotations[spriteIndex] = _rotation;
                }
            }
        }

        private float _spriteSize = 0.8f;

        public float spriteSize
        {
            get => _spriteSize;
            set
            {
                _spriteSize = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.spriteSizes[spriteIndex] = _spriteSize;
                }
            }
        }

        public bool flip = false;

        public int dircet = -1;

        private float _speed = 1;

        public float speed
        {
            get => _speed;
            set
            {
                _speed = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.speeds[spriteIndex] = _speed;
                }
            }
        }

        private SPRITE_ACTION_ENUM _action = SPRITE_ACTION_ENUM.WALK;

        public SPRITE_ACTION_ENUM action
        {
            get => _action;
            set
            {
                _action = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.actions[spriteIndex] = (int) _action;
                }
            }
        }

        private int _team;
        public int team
        {
            get => _team;
            set
            {
                _team = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.team[spriteIndex] = team;
                }
            }
        }
        
        private Vector3 startPoint;
        private Vector3 dirctPoint;
        public string SpriteName = null;
        public int SpriteNameHashCode = 0;
        public string AltasName {get{ return ptSystemNode.AltasName; } }

        public float spriteScale;

        private float _alpha = 1;

        public float alpha
        {
            get => _alpha;
            set
            {
                _alpha = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.alphas[spriteIndex] = (byte) (_alpha * 255);
                }
            }
        }

        public bool HasSetColor = false;

        public Color startColor;

        private bool _needRefreshFlag = false;

        public bool NeedRefreshFlag
        {
            get => _needRefreshFlag;
            set
            {
                _needRefreshFlag = value;
                if (ptSystemNode != null)
                {
                    ref var data = ref ptSystemNode.GetData();
                    data.restarts[spriteIndex] = data.restarts[spriteIndex] || value;
                }
            }
        }

        //public string SpriteName => spriteName;
        protected AnimSprite(AnimSpriteCreator m_SystemNode)
        {
            ptSystemNode = m_SystemNode;
        }

        protected void SetSystemNode(AnimSpriteCreator m_SystemNode)
        {
            ptSystemNode = m_SystemNode;
        }

        public AnimSprite()
        {

        }

        public void Init()
        {

        }
        public void Reset()
        {
            Dispose();
        }

        protected void Dispose()
        {
            active = false;
            startLifetime = 0;
            animationPhase = 0f;
            _alpha = 1;
            _changeAction = false;
            _needRefreshFlag = false;

            _position = Vector3.zero;
            _offset = Vector3.zero;
            _rotation = Vector3.zero;
            _spriteSize = 0.8f;

            flip = false;
            dircet = -1;
            _speed = 1;
            _action = SPRITE_ACTION_ENUM.WALK;
            SpriteName = null;
            SpriteNameHashCode = 0;

            if (ptSystemNode != null)
            {
                ref var data = ref ptSystemNode.GetData();
                data.removes[spriteIndex] = true;
                data.restarts[spriteIndex] = false;
            }

            ptSystemNode = null;
            spriteIndex = -1;
        }
    }
}
